{extend name="common@admin_base" /}

{block name="head_extends"}
<style>
    body .layui-tree-skin-blue .layui-tree-branch{color: #0000cc;}
</style>
<script type="application/javascript">
    layui.config({
        base: '/static/js/',
    }).extend({
        authtree: '',
    });
    layui.use(['jquery', 'authtree', 'form'], function(){
        var $ = layui.jquery;
        var authtree = layui.authtree;
        var form = layui.form;
        $.ajax({
            url: '{$_form.admin_group_authlist}{$group.id}',
            dataType: 'json',
            success: function(data){
                // 渲染时传入渲染目标ID，树形结构数据（具体结构看样例，checked表示默认选中），以及input表单的名字
                authtree.render('#auths', data.data, {inputname: 'authids[]', layfilter: 'lay-check-auth', openall: false});
                form.on('checkbox(lay-check-auth)', function(data){
                    // 注意这里：需要等待事件冒泡完成，不然获取叶子节点不准确。
                    setTimeout(function(){
                        // 获取选中的叶子节点
                        var leaf = authtree.getLeaf('#auths');
                        console.log(leaf);
                    }, 100);
                });
                form.on('submit(voSubmit)',function (data) {
                    console.log(data.field);
                    $.ajax({
                        url:'{$_form.admin_group_updateGroupAuths}{$group.id}'
                        ,data:data.field
                        ,method:'post'
                        ,success:function (data) {
                            console.log(data);
                            if (typeof data == 'string'){
                                data = JSON.parse(data);
                            }
                            parent.layer.alert(data.info);
                            if (data.msg == 1){
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        }
                    });
                    return false;
                });
            }
        })
    });
</script>
{/block}
{block name="body"}

<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-md6 layui-col-md-offset1">
            <fieldset class="layui-elem-field layui-field-title"><legend>权限树扩展分享</legend></fieldset>
            <!-- 此扩展能递归渲染一个权限树，点击深层次节点，父级节点中没有被选中的节点会被自动选中，单独点击父节点，子节点会全部 选中/去选中 -->
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">组名称</label>
                    <div class="layui-input-block">
                        {$group.groupName}
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择权限</label>
                    <div class="layui-input-block">
                        <div id="auths"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button lay-submit class="layui-btn" lay-filter="voSubmit" type="submit">提交</button>
                        <button class="layui-btn layui-btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


{/block}